package com.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wms.dao.WProductDao;
import com.wms.dao.WStockDao;
import com.wms.dao.WStoreDao;
import com.wms.entity.*;
import com.wms.dao.WEntryStoreDao;
import com.wms.mytool.Result;
import com.wms.service.CommonService;
import com.wms.service.WEntryStoreService;
import com.wms.service.common.WStockCommonService;
import com.wms.vo.WEntryStoreInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.Date;
import java.util.List;

/**
 * 入库单表(WEntryStore)表服务实现类
 *
 * @author makejava
 * @since 2023-10-22 21:32:42
 */
@Service("wEntryStoreService")
public class WEntryStoreServiceImpl implements WEntryStoreService {
    @Resource
    private WEntryStoreDao wEntryStoreDao;
    @Resource
    private WStockDao wStockDao;
    @Resource
    private CommonService commonService;

    /**
     * 查询所有入库单
     * @return 实例对象
     */
    @Override
    public Result selectAll() {
        List<WEntryStoreInfo> entryStoreInfos = this.wEntryStoreDao.selectAll();
        if (entryStoreInfos!=null){
            return new Result(200,entryStoreInfos,"查询成功");
        }else{
            return new Result(400,null,"查询失败");
        }
    }

    /**
     * 新增入库信息
     * @param wEntryStore 入库信息
     * @return 实例对象
     */
    @Override
    public Result addInStore(List<WEntryStore> wEntryStore) {
        //查询所有入库单号
        int[] arr = this.wEntryStoreDao.selectAllInId();
        int max=0;
        //入库单号存在
        if(arr.length>0){
            for (int i = 0; i < arr.length; i++) {
                if(arr[i]>max){
                    max=arr[i];
                }
            }
        }else{
            //入库单号不存在，从10000开始
            max=10000;
        }
        Date date = new Date();
        for (WEntryStore entryStore : wEntryStore) {

            Result result=this.commonService.updateStock(entryStore.getStoreId(), entryStore.getPid(), entryStore.getPNum(), true);
            if(result.getCode()==200){
//                入库单号递增
                    entryStore.setInId(max+1);
                    entryStore.setCreateTime(date);
                    //新增入库信息
                    this.wEntryStoreDao.insert(entryStore);
                    //查询仓库是否有库存
                    WStock wStock = this.wStockDao.selectByPidSid(entryStore.getPid(), entryStore.getStoreId());
                    //有库存，库存量增加
                    if(wStock!=null){
                        wStock.setNum(wStock.getNum()+entryStore.getPNum());
                        wStock.setMender(entryStore.getCreator());
                        wStock.setUpdateTime(date);
                        this.wStockDao.updateById(wStock);
                    }else{
                        //无库存，新增
                        WStock wStock1=new WStock();
                        wStock1.setPid(entryStore.getPid());
                        wStock1.setSid(entryStore.getStoreId());
                        wStock1.setNum(entryStore.getPNum());
                        wStock1.setCreator(entryStore.getCreator());
                        wStock1.setMender(entryStore.getCreator());
                        wStock1.setCreateTime(date);
                        wStock1.setUpdateTime(date);
                        this.wStockDao.insert(wStock1);
                    }
            }else{
                return result;
            }
//            //仓库库存增加
//            QueryWrapper<WStore> qw = Wrappers.query();
//            qw.eq("store_id", entryStore.getStoreId());
//            WStore store = this.wStoreDao.selectOne(qw);
//            if (store.getUseNum()+entryStore.getPNum()> store.getMaxNum()){
//                return new Result(400,null,"超过仓库最大库存");
//            }else {
//                //商品库存增加
//                QueryWrapper<WProduct> query = Wrappers.query();
//                query.eq("product_id", entryStore.getPid());
//                WProduct product = this.wProductDao.selectOne(query);
//                if(product.getNum()+entryStore.getPNum()> product.getMaxNum()){
//                    return new Result(400,null,"超过商品最大库存");
//                }else{
//                    product.setNum(product.getNum()+entryStore.getPNum());
//                    this.wProductDao.updateById(product);
//                    store.setUseNum(store.getUseNum()+entryStore.getPNum());
//                    this.wStoreDao.updateById(store);
//                    //入库单号递增
//                    entryStore.setInId(max+1);
//                    entryStore.setCreateTime(date);
//                    //新增入库信息
//                    this.wEntryStoreDao.insert(entryStore);
//                    //查询仓库是否有库存
//                    WStock wStock = this.wStockDao.selectByPidSid(entryStore.getPid(), entryStore.getStoreId());
//                    //有库存，库存量增加
//                    if(wStock!=null){
//                        wStock.setNum(wStock.getNum()+entryStore.getPNum());
//                        wStock.setMender(entryStore.getCreator());
//                        wStock.setUpdateTime(date);
//                        this.wStockDao.updateById(wStock);
//                    }else{
//                        //无库存，新增
//                        WStock wStock1=new WStock();
//                        wStock1.setPid(entryStore.getPid());
//                        wStock1.setSid(entryStore.getStoreId());
//                        wStock1.setNum(entryStore.getPNum());
//                        wStock1.setCreator(entryStore.getCreator());
//                        wStock1.setMender(entryStore.getCreator());
//                        wStock1.setCreateTime(date);
//                        wStock1.setUpdateTime(date);
//                        this.wStockDao.insert(wStock1);
//                    }
//                }
//
//            }

        }
       return new Result(200,"","添加成功");
    }

    /**
     * 删除入库信息
     * @param id 入库id
     * @return 实例对象
     */
    @Override
    public Result deleteInStore(Integer id) {
        QueryWrapper<WEntryStore> qw = Wrappers.query();
        qw.eq("id", id);
        int i = this.wEntryStoreDao.delete(qw);
        if (i==1){
            return new Result(200,i,"删除成功");
        }else{
            return new Result(400,null,"删除失败");
        }
    }
}
